package com.fpt.edu.vn.h2tv.service;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.fpt.edu.vn.h2tv.common.AppConfiguration;
import com.fpt.edu.vn.h2tv.jaxb.BookStoreInformationJAXB;

public class BookStoreInfomationService {
    //public static File FILE =  new File("D:/Study/XML/xml-project-group/trunk/backend/book-service/src/main/resources/companyInformation.xml");
    public static Object getBookStoreInfo(){
        BookStoreInformationJAXB info = new BookStoreInformationJAXB();
        try {
            JAXBContext jc = JAXBContext.newInstance(BookStoreInformationJAXB.class);
            Unmarshaller u = jc.createUnmarshaller();
            
            BookStoreInformationJAXB infoFromFile = (BookStoreInformationJAXB) u.unmarshal(new File(AppConfiguration.informationConfigurationFile));
            
            info.setCompanyName(infoFromFile.getCompanyName());
            info.setAddress(infoFromFile.getAddress());
            info.setPhone(infoFromFile.getPhone());
            info.setEmail(infoFromFile.getEmail());
            info.setDescription(infoFromFile.getDescription());
            
        } catch (JAXBException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }

        return info;
    }

    public static String updateInformation(String name, String address, String phone, String email, String desc){
        String noti = "";
        
        BookStoreInformationJAXB info = new BookStoreInformationJAXB();
        info.setCompanyName(name);
        info.setAddress(address);
        info.setPhone(phone);
        info.setEmail(email);
        info.setDescription(desc);
        
        try {
            JAXBContext context = JAXBContext.newInstance(info.getClass());
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(info, new File(AppConfiguration.informationConfigurationFile));
            noti = "update bookstore information successful!";
        } catch (JAXBException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            noti = "update bookstore information failed!";
        }
        return noti;
    }
    
    
    
}
